package com.sh.business.config;

import com.rabbitmq.client.Channel;
import com.sh.business.domain.dto.DelayMessageDto;
import com.sh.business.service.facade.TransactionServiceFacade;
import com.sh.business.service.facade.UserFoundServiceFacade;
import com.sh.business.service.facade.UserServiceFacade;
import com.sh.common.core.enums.MessageTypeEnum;
import com.sh.common.core.enums.MqQueueEnum;
import com.sh.common.json.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Objects;

import static com.sh.common.core.enums.MqQueueEnum.TTL_DAILY_DYNAMICS_DIRECT;

/**
 * @author dongGe_zzz
 * @date 2024-03-22 19:47
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class RabbitmqReceiver {

    private final TransactionServiceFacade transactionServiceFacade;
    private final UserFoundServiceFacade userFoundServiceFacade;
    private final UserServiceFacade userServiceFacade;


    /**
     * 异步消息
     * @param message
     * @param txHash
     * @param channel
     */
    @RabbitListener(queues = "aleo.callback.direct.queue")
    public void callbackListener(Message message, String txHash, Channel channel) throws IOException {
        log.info("收到异步消息:{}", txHash);

        MessageProperties messageProperties = message.getMessageProperties();
        long tag = messageProperties.getDeliveryTag();
        try {

            userServiceFacade.handleInvestNotify(txHash);

            log.info("异步消息已消费:{}", txHash);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            channel.basicAck(tag, false);
        }

    }

    /**
     * 延迟查询充值交易消息
     * @param message
     * @param msg
     * @param channel
     */
    @RabbitListener(queues = "aleo.ttl.direct.queue")
    public void queryTxListener(Message message, String msg, Channel channel) throws IOException {
        log.info("收到延迟查询充值交易消息:{}", msg);

        MessageProperties messageProperties = message.getMessageProperties();
        long tag = messageProperties.getDeliveryTag();

        try {
            DelayMessageDto delayMessageDto = JsonUtils.parseObject(msg, DelayMessageDto.class);

            transactionServiceFacade.checkTransactionByHash(delayMessageDto);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            channel.basicAck(tag, false);
        }

    }

    /**
     * 24小时后执行szpn动态奖励
     * @param message
     * @param orderSn
     * @param channel
     */
    @RabbitListener(queues = "aleo.ttl.daily.dynamics.direct.queue")
    public void szpnDailyDynamics(Message message, String orderSn, Channel channel) throws IOException {
        log.info("收到24小时后执行szpn动态奖励消息:{}", orderSn);

        MessageProperties messageProperties = message.getMessageProperties();
        long tag = messageProperties.getDeliveryTag();

        try {
            // 不再使用延迟消息处理，改用定时任务
            // userServiceFacade.szpnDynamics(orderSn);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            channel.basicAck(tag, false);
        }

    }

    /**
     * 24小时后执行szpn动态奖励 -外部投资
     * @param message
     * @param orderSn
     * @param channel
     */
    @RabbitListener(queues = "aleo.ttl.daily.dynamics.out.direct.queue")
    public void szpnDailyDynamicsOut(Message message, String orderSn, Channel channel) throws IOException {
        log.info("收到24小时后执行szpn动态奖励消息-外部投资:{}", orderSn);

        MessageProperties messageProperties = message.getMessageProperties();
        long tag = messageProperties.getDeliveryTag();

        try {
            // 不再使用延迟消息处理，改用定时任务
            //userServiceFacade.szpnDynamicsOut(orderSn);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            channel.basicAck(tag, false);
        }

    }

}
